Skip to main content

HEAD 💀

HEAD is just a special pointer which points to current state (commit) of our repo.

Reflog

reflog is a special log which only stores the moving history of HEAD pointer.

Why it is useful ?

🤔Problem

This will be an interesting set of tasks to do, but can be quite useful in understanding reflog.

  1. create a new branch off of master, call it baz.
  2. add one commit to baz. Do it in a new file baz.md
  3. switch back to master and delete baz (git branch -D baz from earlier)
  4. can you bring back from the dead the commit sha of baz?

Moving HEAD

We can also move HEAD relative to its position, to walk back commits. We use a special syntax with ~n to give relative position of commits wrt to current commit.


git checkout HEAD~3

Reset

We can reset current state of repo to some previous state.

there are 2 options to reset to a previous state


git reset --hard <SHA>

# or

git reset --soft <SHA>

--hard: will reset your repo state to previous commit, and also changes will be lost. ("😏 except from reflog")

--soft: will reset your repo state to previous commit, but changes will reflect back into tracked stage.

Revert

Revert is like anti-commit of <provided_commit>, basically it will not change any history but will add an anti-change which will remove the changes of the <provided_commit>.


git revert <SHA>

Clean

git reset is good to move HEAD to some previous state, but what about we want to clean our repo to its last commit. Basically we want to discard the changes. to do that we use

git clean -f -d -x

Restore

To restore some changes in a file.

git restore <file_name>

cherry-pick 🍒

We can bring only one commit from one branch to another branch ny cherry picking that commit.


git cherry-pick <commit SHA>

info

cherry-pick is one of the most useful command of git, because there will certainly be cases where a change has such an bad divergence and the merge conflict is so bad, that only thing is to hand move (cherry-pick) the commits across branches.